package ru.java2e;

import java.awt.*;
//import java.awt.event.KeyEvent;
import java.util.Vector;


//import javax.swing.ImageIcon;
//import javax.swing.JFrame;
import javax.swing.JPanel;

enum Direction
{
	Up, Right, Down, Left;
}

enum UnitGroup
{
	NoneGroup, PlayerGroup, BotGroup;
}

public class Tank extends JPanel
{
	int x;
	int y;
	Direction direct;
	int hp;
	UnitGroup group;
	int explosionLvl;
	int speedBullet;
	
	Image imgTank;
	Image imgDefaultUp, imgDefaultRight, imgDefaultDown, imgDefaultLeft;
	Image imgExplosion;
	
	Vector<Bullet> bullets = new Vector<Bullet>();
	
	public Tank(int x, int y)
	{
		setLocTank(x, y);
		setDirectTank(Direction.Up);
		this.hp = 100;
		this.group = UnitGroup.NoneGroup;
		this.explosionLvl = 0;
		this.speedBullet = 2;
	}
	
	public void setHP(int hp) 
	{
		this.hp = hp;
	}
	
	public int getHP() 
	{
		return hp;
	}
	
	public boolean takeDamage(int damage) 
	{
		this.hp -= damage;
		if(this.hp <= 0)
		{
			hp = 0;
			return true;
		}
		else
			return false;
	}
	
	public void percentTurnTank()
	{
		int turn = (int) (Math.random()*3);
			
			if(turn == 0)
			{
				if(getDirectTank() == Direction.Up)
					setDirectTank(Direction.Right);
				else if(getDirectTank() == Direction.Right)
					setDirectTank(Direction.Down);
				else if(getDirectTank() == Direction.Down)
					setDirectTank(Direction.Left);
				else if(getDirectTank() == Direction.Left)
					setDirectTank(Direction.Up);
			}
			else if (turn == 1)
			{
				if(getDirectTank() == Direction.Up)
					setDirectTank(Direction.Down);
				else if(getDirectTank() == Direction.Right)
					setDirectTank(Direction.Left);
				else if(getDirectTank() == Direction.Down)
					setDirectTank(Direction.Up);
				else if(getDirectTank() == Direction.Left)
					setDirectTank(Direction.Right);
			}
			else if (turn == 2)
			{
				if(getDirectTank() == Direction.Up)
					setDirectTank(Direction.Left);
				else if(getDirectTank() == Direction.Right)
					setDirectTank(Direction.Up);
				else if(getDirectTank() == Direction.Down)
					setDirectTank(Direction.Right);
				else if(getDirectTank() == Direction.Left)
					setDirectTank(Direction.Down);
			}
		
}
		
	public void setTankImages(Image imgUp, Image imgRight, Image imgDown, Image imgLeft)
	{
		this.imgDefaultUp = imgUp;
		this.imgDefaultRight = imgRight;
		this.imgDefaultDown = imgDown;
		this.imgDefaultLeft = imgLeft;
		this.imgTank = imgDefaultUp;
	}
	
	public void setDirectTank(Direction direct)
	{
		this.direct = direct;
		
		if(direct == Direction.Up)
			this.imgTank = this.imgDefaultUp;
		else if(direct == Direction.Right)
			this.imgTank = this.imgDefaultRight;
		else if(direct == Direction.Down)
			this.imgTank = this.imgDefaultDown;
		else if(direct == Direction.Left)
			this.imgTank = this.imgDefaultLeft;
	}
	
	public Direction getDirectTank()
	{
		return direct;
	}
	
	public Image getTankImage()
	{
		return this.imgTank;
	}
	
	public void setLocTank(int x, int y)
	{
		this.x = x;
		this.y = y;
	}
	
	public void setGroup(UnitGroup group)
	{
		this.group = group;
	}
	
	public UnitGroup getGroup()
	{
		return group;
	}
	
	public int getX()
	{
        return x;
    }
 
    public int getY()
    {
        return y;
    }    
    
    public void fire()
    {
    	if(!Map.autoFire && bullets.size() == 1)
    		return;
    				
    	int rx = 0, ry = 0;
    	
    	if(direct == Direction.Up)
    		ry -= speedBullet;
    	else if(direct == Direction.Right)
    		rx += speedBullet;
    	else if(direct == Direction.Down)
    		ry += speedBullet;
    	else if(direct == Direction.Left)
    		rx -= speedBullet;
    	
    	int x = (Map.sizeCell*this.x) + (Map.sizeCell/2);
    	int y = (Map.sizeCell*this.y) + (Map.sizeCell/2);
    	Bullet bullet = new Bullet(x, y, rx, ry);
    	bullet.setSender(this);
    	bullets.add(bullet);
    }
    
    public Bullet[] getArrayBullets()
    {
		return bullets.toArray(new Bullet[0]);
    }
    
    public void moveAllBullet()
    {
    	for(int k = 0; k < bullets.size(); k++)
    	{
    		bullets.elementAt(k).moveB();
    	}
    }
    
    public boolean deleteBullet(int index)
    {
    	int num = bullets.size();
    	bullets.removeElementAt(index);
    	if(num != bullets.size())
    		return true;
    	else
    		return false;
    }   
}